﻿//********************************************************************************//
//创建日期：<创建日期，2016-04-08>
//创建作者：<朱大鹏，zhudapeng@shanghai3h.com>
//功能说明：抄表数据修改
//********************************************************************************//
using System;
using System.Linq;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Web.Class;
using Platform.Common;

namespace Web.Pages.YongShui._4.ChaoBiaoKZ
{
    public partial class ChaoBiaoSJXG : BasePage
    {
        #region 属性
        #endregion

        #region 变量
        /// <summary>
        /// 日志操作
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();
        SpHelper _sphelper = new SpHelper();
        #endregion

        #region  事件
        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
                SetControlEnable();
                BtnModify.Visible = false;
            }
        }
        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (txtNo.Text.ToString().Trim().Length > 0)
            {
                BindControlValue();
                SetControlEnable();

            }
        }
        /// <summary>
        /// 修改按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnModify_Click(object sender, EventArgs e)
        {

            TextChaoBiaoRQ.ReadOnly = false;
            TextBenYueCM.ReadOnly = false;
            TextBenYueSL.ReadOnly = false;
            DrpChaoBiaoZT.ReadOnly = false;
            TextChaoBiaoRQ.Focus();

            BtnSave.Visible = true;
            BtnCannle.Visible = true;
            BtnModify.Visible = false;
        }
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSave_Click(object sender, EventArgs e)
        {
            DateTime date;
            int chaoma = 0;
            if (!DateTime.TryParse(TextChaoBiaoRQ.Text, out date))
            {
                PageTools.AlertMsg(this, "选中抄码日期错误，请重新选择！");
                TextChaoBiaoRQ.Focus();
                return;
            }
            if (!int.TryParse(TextBenYueCM.Text, out chaoma))
            {
                PageTools.AlertMsg(this, "输入抄码不正确，请重新输入！");
                TextBenYueCM.Focus();
                return;
            }
            if (!int.TryParse(TextBenYueSL.Text, out chaoma))
            {
                PageTools.AlertMsg(this, "本月水量输入错误，请重新输入！");
                TextBenYueCM.Focus();
                return;
            }
            if ((Convert.ToDecimal(TextBenYueCM.Text) - Convert.ToDecimal(TextShangYueCM.Text.Trim().Length == 0 ? "0" : TextShangYueCM.Text)) != Convert.ToDecimal(TextBenYueSL.Text))
            {
                PageTools.AlertMsg(this, "输入抄码不正确，请重新输入！");
                TextBenYueCM.Focus();
                return;
            }
            if (!int.TryParse(TextShangYueSL.Text, out chaoma))
            {
                PageTools.AlertMsg(this, "较对水量输入错误，请重新输入！");
                TextBenYueCM.Focus();
                return;
            }
            int jdsl = 0;
            int [] arrzt=new int[]{0,1,5,6,7,8,9};
            if(DrpChaoBiaoZT.Value.ToString()=="2"||DrpChaoBiaoZT.Value.ToString()=="3")
            {
                jdsl = Convert.ToInt32(TextBenYueSL.Text);
            }
            else if (arrzt.Contains<int>(Convert.ToInt32((DrpChaoBiaoZT.Value))))
            {
                int days = DateTime.DaysInMonth(DateTime.Parse(TextShangYueCBRQ.Text).Year, DateTime.Parse(TextShangYueCBRQ.Text).Month);
                TimeSpan newdays = DateTime.Parse(TextChaoBiaoRQ.Text) - DateTime.Parse(TextShangYueCBRQ.Text);
                int days2 = newdays.Days;
                jdsl = (int)Math.Round(Convert.ToInt32(TextBenYueSL.Text) * days / double.Parse(days2.ToString()), 0);
            }
            else
            {
                jdsl = Convert.ToInt32(TextShangYueSL.Text);
            }
            YS_CB_SHUIBIAOXXBLL bllS = new YS_CB_SHUIBIAOXXBLL();

            YS_CB_SHUIBIAOXX sbxx = bllS.SelectYS_CB_SHUIBIAOXX(Convert.ToDecimal(id.Text));

            DataSet ds = _sphelper.P_YS_CB_UPDATESHUIBIAOXX(DrpSite.Value.ToString(), txtNo.Text.Trim(), Convert.ToDecimal(TextBenYueCM.Text), DateTime.Parse(TextChaoBiaoRQ.Text), Convert.ToDecimal(TextBenYueSL.Text), Convert.ToDecimal(DrpChaoBiaoZT.Value), jdsl);
            

            _SYS_LOGBLL.Log<YS_CB_SHUIBIAOXX>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "抄表开账-抄表数据修改", OperateState.Modify, sbxx, null);
            BindControlValue();
            SetControlEnable();
            BtnModify.Visible = true;


        }
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            if (e.Parameter == "jdsl")
            {
                int jdsl = 0;
                int code = 0;
                int test=0;
                DateTime d;
                if (int.TryParse(TextBenYueSL.Text, out test) && DateTime.TryParse(TextChaoBiaoRQ.Text, out d) && DateTime.TryParse(TextShangYueCBRQ.Text,out d))
                {

                    int[] arrzt = new int[] { 0, 1, 5, 6, 7, 8, 9 };
                    if (DrpChaoBiaoZT.Value.ToString() == "2" || DrpChaoBiaoZT.Value.ToString() == "3")
                    {
                        jdsl = Convert.ToInt32(TextBenYueSL.Text);
                        code = 1;
                    }
                    else if (arrzt.Contains<int>(Convert.ToInt32((DrpChaoBiaoZT.Value))))
                    {
                        int days = DateTime.DaysInMonth(DateTime.Parse(TextShangYueCBRQ.Text).Year, DateTime.Parse(TextShangYueCBRQ.Text).Month);
                        TimeSpan newdays = DateTime.Parse(TextChaoBiaoRQ.Text) - DateTime.Parse(TextShangYueCBRQ.Text);
                        int days2 = newdays.Days;
                        jdsl = (int)Math.Round(Convert.ToInt32(TextBenYueSL.Text) * days / double.Parse(days2.ToString()), 0);
                        code = 2;
                    }
                    else
                    {
                        code = -1;
                    }
                }
                e.Result = e.Parameter+"|"+code+"|"+jdsl;
            }
        }
        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnCannle_Click(object sender, EventArgs e)
        {
            
            SetControlEnable();
            BtnModify.Visible = false;
            ClearControlValue();
            
        }
        #endregion

        #region 方法
        /// <summary>
        /// 页面首次加载绑定控件
        /// </summary>
        private void InitControl()
        {
            //获取当前用户站点信息
            DataTable dtZhandian = BusinessTool.GetTextAndValue("view_as_userinfo", "ZHANDIANMC", "ZHANDIANBH", null, string.Format("account='{0}'", LoginInfo.CurrentUser.UserID)).Tables[0];
            string zhandianBH = "";
            if (dtZhandian != null && dtZhandian.Rows.Count > 0)
            {
                zhandianBH = dtZhandian.Rows[0]["VALUE"].ToString();
            }
            string yd = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
            TextStart.Text = yd.Substring(0,4);
            TextEnd.Text = yd.Substring(4, 2);
            //加载站点
            BLL.YS_JX_ZHANDIANBLL bllT = new YS_JX_ZHANDIANBLL();
            DataTable rs = bllT.SelectYS_JX_ZHANDIANsDynamic(" S_ZHANDIANBH='"+zhandianBH+"' or '00'='"+zhandianBH+"'", null);
            foreach (DataRow dr in rs.Rows)
            {
                dr["S_ZHANDIANMC"] = dr["S_ZHANDIANBH"].ToString() +"-"+ dr["S_ZHANDIANMC"].ToString();
            }
           
            this.DrpSite.DataSource = rs;
            this.DrpSite.TextField = "S_ZHANDIANMC";
            this.DrpSite.ValueField = "S_ZHANDIANBH";
            this.DrpSite.DataBind();

            this.DrpSite.SelectedIndex = 0;
            if (zhandianBH.Trim().Length > 0)
            {
                DrpSite.Value = zhandianBH;
            }
            
            
        }
        /// <summary>
        /// 查询后绑定页面控件
        /// </summary>
        private void BindControlValue()
        {

            

            DataTable dtWords = WordsHelper.GetXiaoLeiListByWordsID(6);
            DrpSBLX.DataSource = dtWords;
            DrpSBLX.TextField = "WordsContent";
            DrpSBLX.ValueField = "WordsValue";
            DrpSBLX.DataBind();

            DataTable dtI_CHAOBIAOZT = WordsHelper.GetXiaoLeiListByWordsID(9);
            if (dtI_CHAOBIAOZT != null && dtI_CHAOBIAOZT.Rows.Count > 0)
            {
                foreach (DataRow dr in dtI_CHAOBIAOZT.Rows)
                {
                    dr["WordsContent"] = dr["WordsValue"].ToString() + "-" + dr["WordsContent"].ToString();
                }
            }
            DrpChaoBiaoZT.DataSource = dtI_CHAOBIAOZT;
            DrpChaoBiaoZT.TextField = "WordsContent";
            DrpChaoBiaoZT.ValueField = "WordsValue";
            DrpChaoBiaoZT.DataBind();

            

            YS_CB_SHUIBIAOXXBLL bllS = new YS_CB_SHUIBIAOXXBLL();
            string sqlwhere = "  S_SHUIBIAOBH='{1}' and I_LEIXING=0 and  I_JLZT=0 and (S_ZHANDIANBH='{0}' or '00'='{0}' ) and Exists(Select 1 from YS_CB_YONGHUSBGX c where c.S_SHUIBIAOBH=YS_CB_SHUIBIAOXX.S_SHUIBIAOBH and  c.I_JLZT=0)  and  I_TYPE < 2 ";
            sqlwhere = string.Format(sqlwhere, DrpSite.Value, txtNo.Text);
            DataTable dt = bllS.SelectYS_CB_SHUIBIAOXXesDynamic(sqlwhere, "");
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    TextSBAdress.Text = dr["S_DIZHI"].ToString();

                    TextSBYH.Text = dr["S_MINGCHENG"].ToString();

                    TextWaterPrice.Text = dr["N_YSJ"].ToString();


                    TextCeHao.Text = dr["S_CH"].ToString();
                    TextGuanWangKH.Text = dr["S_KUAIHAO"].ToString();
                    DrpSBLX.Value = dr["I_TYPE"].ToString();

                    TextKouJing.Text = dr["I_KOUJING"].ToString();
                    DateTime riqi;
                    if (DateTime.TryParse(dr["D_CHAOBIAO"].ToString(), out riqi))
                    {
                        TextChaoBiaoRQ.Date = Convert.ToDateTime(dr["D_CHAOBIAO"]);
                    }
                    if (DateTime.TryParse(dr["D_SHANGYUE"].ToString(), out riqi))
                    {
                        TextShangYueCBRQ.Date = Convert.ToDateTime(dr["D_SHANGYUE"]);
                    }

                    TextBenYueCM.Text = dr["I_BENYUECM"].ToString();
                    TextShangYueCM.Text = dr["I_SHANGYUECM"].ToString();
                    TextBenYueSL.Text = dr["I_BENYUESL"].ToString();

                    TextShangYueSL.Text = dr["I_JIAODUISL"].ToString();
                    DrpChaoBiaoZT.Value = dr["I_CHAOBIAOZT"].ToString() == "" ? "0" : dr["I_CHAOBIAOZT"].ToString();
                    TextShuiSiZT.Text = dr["S_CHAOBIAOZT"].ToString();

                    TextGuJiCS.Text = dr["I_GUJICS"].ToString();

                    

                    id.Text = dr["ID"].ToString();
                    YS_CB_YONGHUSBGXBLL gxbll = new YS_CB_YONGHUSBGXBLL();

                    DataTable yxsbgx = gxbll.SelectYS_CB_YONGHUSBGXesDynamic(string.Format(" S_SHUIBIAOBH='{0}' and i_shuibiaozt=0 and i_leixing=0 ", dr["S_SHUIBIAOBH"].ToString()), "");
                    if (yxsbgx != null && yxsbgx.Rows.Count > 0)
                    {
                        YS_YG_YONGHUXXBLL yhxxbll = new YS_YG_YONGHUXXBLL();
                        string yhxxwhere = string.Format(" S_YONGHUH='{0}'",yxsbgx.Rows[0]["S_YONGHUBH"].ToString());
                        DataTable yhxx = yhxxbll.SelectYS_YG_YONGHUXXesDynamic(yhxxwhere,"");
                        if (yhxx != null && yhxx.Rows.Count > 0)
                        {
                            TextUserNo.Text = yhxx.Rows[0]["S_YONGHUH"].ToString();
                            TextUserName.Text = yhxx.Rows[0]["S_YONGHUM"].ToString();
                        }
                    }

                    
                }
                
                BtnModify.Visible = true;
            }
            else
            {
                PageTools.AlertMsg(this, "没有符合条件的水表，请重新选择！");
                ClearControlValue();
                return;
            }
        }
        /// <summary>
        /// 清空页面控件值
        /// </summary>
        private void ClearControlValue()
        {
            BtnModify.Visible = false;
            TextUserNo.Text = "";
            TextUserName.Text = "";

            TextSBAdress.Text = "";
            TextSBYH.Text = "";
            TextWaterPrice.Text = "";

            TextCeHao.Text = "";
            TextGuanWangKH.Text = "";
            DrpSBLX.SelectedIndex = 0;

            TextKouJing.Text = "";
            TextChaoBiaoRQ.Text = "";
            TextShangYueCBRQ.Text = "";

            TextBenYueCM.Text = "";
            TextShangYueCM.Text = "";
            TextBenYueSL.Text = "";

            TextShangYueSL.Text = "";
            DrpChaoBiaoZT.SelectedIndex = 0;
            TextShuiSiZT.Text = "";

            TextGuJiCS.Text = "";
           

            BtnSave.Visible = false;
            BtnCannle.Visible = false;

        }
        /// <summary>
        /// 设置禁用页面指定的控件
        /// </summary>
        private void SetControlEnable()
        {
            PageTools.SetDisabled(TextUserNo,true);
            PageTools.SetDisabled(TextUserName, true);
            PageTools.SetDisabled(TextSBAdress, true);

            PageTools.SetDisabled(TextSBYH, true);
            PageTools.SetDisabled(TextWaterPrice, true);
            PageTools.SetDisabled(TextCeHao, true);

            PageTools.SetDisabled(TextGuanWangKH, true);
            
            PageTools.SetDisabled(TextKouJing, true);

           
            PageTools.SetDisabled(TextShangYueCM, true);
            

            PageTools.SetDisabled(TextShangYueSL, true);
           
            PageTools.SetDisabled(TextShuiSiZT, true);

            PageTools.SetDisabled(TextGuJiCS, true);
            PageTools.SetDisabled(TextStart, true);
            PageTools.SetDisabled(TextEnd, true);

            DrpSBLX.ReadOnly = true;
            DrpChaoBiaoZT.ReadOnly = true;
            TextChaoBiaoRQ.ReadOnly = true;
            TextShangYueCBRQ.ReadOnly = true;
            TextBenYueCM.ReadOnly = true;
            TextBenYueSL.ReadOnly = true;


            BtnSave.Visible = false;
            BtnCannle.Visible = false;
        }
        #endregion

       
       
    }
}